package com.ruoyi.SSL.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.SSL.domain.SslTrusteeshipService;
import com.ruoyi.SSL.mapper.SslTrusteeshipServiceMapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.SSL.mapper.SslTrusteeshipServiceDomainMapper;
import com.ruoyi.SSL.domain.SslTrusteeshipServiceDomain;
import com.ruoyi.SSL.service.ISslTrusteeshipServiceDomainService;
import com.ruoyi.common.core.text.Convert;

/**
 * 托管域名管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@Service
public class SslTrusteeshipServiceDomainServiceImpl implements ISslTrusteeshipServiceDomainService
{
    @Autowired
    private SslTrusteeshipServiceDomainMapper sslTrusteeshipServiceDomainMapper;

    @Autowired
    private SslTrusteeshipServiceMapper trusteeshipServiceMapper;

    /**
     * 查询托管域名管理
     *
     * @param id 托管域名管理主键
     * @return 托管域名管理
     */
    @Override
    public SslTrusteeshipServiceDomain selectSslTrusteeshipServiceDomainById(Long id)
    {
        return sslTrusteeshipServiceDomainMapper.selectSslTrusteeshipServiceDomainById(id);
    }

    /**
     * 查询托管域名管理列表
     *
     * @param sslTrusteeshipServiceDomain 托管域名管理
     * @return 托管域名管理
     */
    @Override
    public List<SslTrusteeshipServiceDomain> selectSslTrusteeshipServiceDomainList(SslTrusteeshipServiceDomain sslTrusteeshipServiceDomain)
    {
        return sslTrusteeshipServiceDomainMapper.selectSslTrusteeshipServiceDomainList(sslTrusteeshipServiceDomain);
    }

    /**
     * 新增托管域名管理
     *
     * @param sslTrusteeshipServiceDomain 托管域名管理
     * @return 结果
     */
    @Override
    public int insertSslTrusteeshipServiceDomain(SslTrusteeshipServiceDomain sslTrusteeshipServiceDomain)
    {
        sslTrusteeshipServiceDomain.setCreateTime(DateUtils.getNowDate());
        return sslTrusteeshipServiceDomainMapper.insertSslTrusteeshipServiceDomain(sslTrusteeshipServiceDomain);
    }

    /**
     * 修改托管域名管理
     *
     * @param sslTrusteeshipServiceDomain 托管域名管理
     * @return 结果
     */
    @Override
    public int updateSslTrusteeshipServiceDomain(SslTrusteeshipServiceDomain sslTrusteeshipServiceDomain)
    {
        sslTrusteeshipServiceDomain.setUpdateTime(DateUtils.getNowDate());
        return sslTrusteeshipServiceDomainMapper.updateSslTrusteeshipServiceDomain(sslTrusteeshipServiceDomain);
    }

    /**
     * 批量删除托管域名管理
     *
     * @param ids 需要删除的托管域名管理主键
     * @return 结果
     */
    @Override
    public int deleteSslTrusteeshipServiceDomainByIds(String ids)
    {
        return sslTrusteeshipServiceDomainMapper.deleteSslTrusteeshipServiceDomainByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除托管域名管理信息
     *
     * @param id 托管域名管理主键
     * @return 结果
     */
    @Override
    public int deleteSslTrusteeshipServiceDomainById(Long id)
    {
        return sslTrusteeshipServiceDomainMapper.deleteSslTrusteeshipServiceDomainById(id);
    }

    public List<Ztree> selectTree(SslTrusteeshipService trusteeshipService){
        List<SslTrusteeshipService> dataList = trusteeshipServiceMapper.selectSslTrusteeshipServiceList(trusteeshipService);
        return initZtree(dataList);
    }

    /**
     * 对象转部门树
     *
     * @param trusteeshipServiceList 服务器列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SslTrusteeshipService> trusteeshipServiceList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SslTrusteeshipService trusteeshipService : trusteeshipServiceList)
        {
            if (UserConstants.DEPT_NORMAL.equals(trusteeshipService.getStatus()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(trusteeshipService.getId());
                ztree.setpId(0L);
                ztree.setName(trusteeshipService.getSerName());
                ztree.setTitle(trusteeshipService.getSerName());
                ztrees.add(ztree);
            }
        }
        Ztree ztree = new Ztree();
        ztree.setId(-1L);
        ztree.setpId(0L);
        ztree.setName("其他【非服务器】");
        ztree.setTitle("其他【非服务器】");
        ztrees.add(ztree);
        return ztrees;
    }

    public void delStsIdIsNull(){
        List<SslTrusteeshipServiceDomain> trusteeshipServiceDomainList = sslTrusteeshipServiceDomainMapper.selectDelList();
        //删除数据
        for (SslTrusteeshipServiceDomain serviceDomain : trusteeshipServiceDomainList) {
            sslTrusteeshipServiceDomainMapper.deleteSslTrusteeshipServiceDomainById(serviceDomain.getId());
        }
    }
}
